#ifndef _SOMB_MESSAGE_H_
#define _SOMB_MESSAGE_H_

#include "somb.h"

#define SOMB_MESSAGE_PAYLOAD_SIZE_MAX        (1024*4)


#define SOMB_MESSAGE_BUFFER_MALLOC(buffer,size) \
{ \
    buffer = malloc(size);\
    memset(buffer,0,size);\
}
#define SOMB_MESSAGE_BUFFER_FREE(buffer) \
{ \
    free(buffer);\
}

#define SOMB_MESSAGE_HEADER_SIZE   (16)
#define SOMB_MESSAGE_HEADER_INIT(header,SubNsId,MsgSeqId,ServiceId,Type,MessageLen)\
{\
    header[0] = 0x00;\
    header[1] = 0x00;\
    header[2] = MsgSeqId&0xFF;\
    header[3] = Type&0xFF;\
    header[4] = (ServiceId&0xFF000000)>>24;\
    header[5] = (ServiceId&0x00FF0000)>>16;\
    header[6] = (ServiceId&0x0000FF00)>>8;\
    header[7] = (ServiceId&0x000000FF);\
    header[8] = (MessageLen&0xFF000000)>>24;\
    header[9] = (MessageLen&0x00FF0000)>>16;\
    header[10] = (MessageLen&0x0000FF00)>>8;\
    header[11] = (MessageLen&0x000000FF);\
    header[12] = (SubNsId&0xFF000000)>>24;\
    header[13] = (SubNsId&0x00FF0000)>>16;\
    header[14] = (SubNsId&0x0000FF00)>>8;\
    header[15] = (SubNsId&0x000000FF);\
}
#define SOMB_MESSAGE_HEADER_DUMP(header)\
{\
    SOMB_LOG_DEBUG("Seq=%d Type=%d ServiceId=0x%08x MessageLen=%d",header[2],header[3], (header[4]<<24) | (header[5]<<16) | (header[6]<<8) | (header[7]), (header[8]<<24) | (header[9]<<16) | (header[10]<<8) | (header[11])) \
}

extern int32_t i32SOMBMessageDecode(int32_t ServiceId,uint8_t field_index,void **payload, void* buffer,uint32_t buffer_size);
extern int32_t i32SOMBMessageEncode(int32_t ServiceId,void *payload, void** buffer,uint32_t *buffer_size);

extern int32_t i32SOMB_Msg_Send_2_Daemon(uint32_t MsgId, void *msgreq,const pb_msgdesc_t * reqfields,uint32_t reqfield_size);
#endif //_SOMB_MESSAGE_H_
